package cn.itcast.nems.studentrecord.service.impl;

import cn.itcast.nems.studentrecord.constant.ReferenceTypeEnum;
import cn.itcast.nems.studentrecord.converter.StudentRecordReferenceConverter;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecord;
import cn.itcast.nems.studentrecord.dao.entity.StudentRecordTransaction;
import cn.itcast.nems.studentrecord.dto.change.*;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordReferenceDTO;
import cn.itcast.nems.studentrecord.service.settlement.SettlementCalculatorRenewal;
import cn.itcast.nems.studentrecord.service.settlement.SettlementResult;
import cn.itcast.nems.studentrecord.service.studentrecord.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author liyong
 * create: 2023-11-14 15:56:22
 **/
@Service
public class StudentRecordRenewalServiceImpl extends StudentRecordChangeTargetWithOrder implements StudentRecordRenewalService {

    StudentRecordRenewalServiceImpl(StudentRecordService studentRecordService,
                                    StudentRecordTransactionService studentRecordTransactionService,
                                    StudentRecordCreateService studentRecordCreateService,
                                    StudentRecordMessageService studentRecordMessageService,
                                    StudentRecordReceivableService studentRecordReceivableService,
                                    OrderApiDelegate orderApiDelegate) {
        super(studentRecordService,
                studentRecordTransactionService,
                studentRecordCreateService,
                studentRecordMessageService,
                studentRecordReceivableService,
                orderApiDelegate);
    }

    @Override
    @Transactional
    public StudentRecordChangeResultDTO renewal(StudentRecordChangeDTO studentRecordChange) {
        StudentRecordChangeResultDTO result = super.change(studentRecordChange, ReferenceTypeEnum.RENEWAL);
        if (result == null) {
            return null;
        }
        if (result.isComplete()) {
            this.renewalComplete(result.getJournalDTO());
        }
        return result;
    }

    @Override
    @Transactional
    public void renewalComplete(StudentRecordJournalDTO studentRecordJournal) {
        super.changeComplete(studentRecordJournal, ReferenceTypeEnum.RENEWAL);

        StudentRecord sr = super.studentRecordService.getById(studentRecordJournal.getOriginStudentRecordId());
        if (sr.getSettlementDate() != null) { // 之前结算过
            StudentRecord update = new StudentRecord();
            update.setId(sr.getId());
            // update.setLockFlag(false);
            update.setRenewalFlag(true);
            super.studentRecordService.updateStudentRecordById(update);
        } else {
            final SettlementResult settlementResult = super.studentRecordService.studentRecordSettlement(studentRecordJournal.getOriginStudentRecordId(),
                    studentRecordJournal.getOriginClazzOutDate(),
                    new SettlementCalculatorRenewal());
            StudentRecordReferenceDTO dto = StudentRecordReferenceConverter.origin(sr, studentRecordJournal);
            logger.info("升级完成，创建基础班学籍流转信息，学籍ID: '{}' {}", sr.getId(), dto);
            super.createStudentRecordTransaction(dto, settlementResult, StudentRecordTransaction.TYPE_OUT);
        }
        studentRecordService.unlock(sr.getId());
    }

    @Override
    StudentRecordChangeChecker createStudentRecordChangeChecker(StudentRecordChangeCheckOriginDTO studentRecordOrigin,
                                                                List<StudentRecordTransactionChangeCheckDTO> transactions) {
        return new StudentRecordChangeRenewalChecker(studentRecordOrigin, transactions);
    }
}
